<?php
#### PAGES MANAGEMENT CONTROLLER ###############################################
class PagesController {

	var $feedback;

	#### CONSTRUCTOR ###########################################################
	function PagesController() {
		Base::getThis();

		// Set the page title
		$this->page_title = _('Pages');
		if (url(3)) $this->page_title .= ' '.ucwords(url(3));

		// Get variables
		variable($this->id, 'id', 'get', url('4'));

		// Make the model ready for use
		$this->model = $this->load->model('pages');

		// The header is to be included module wide
		$this->buildHeader();

		// Perform default function
		invoke_method($this, url(3));
	}

	#### BUILD HEADER ##########################################################
	function buildHeader() {

		// To Exlude this header from being shown on a function set the functions output as
		// "$this->output =" rather than appending to $this->output with "$this->output .="

		// Build the links array
		$links[] = array(_('Config'), url(1, 2).'/config', $this->icon['config']);
		$links[] = array(_('Manage Pages'), url(1, 2), $this->icon['pages_manage']);
		$links[] = array(_('Add a Page'), url(1, 2).'/add', $this->icon['pages_add']);

		// Include categories library, configure categories and return peformed action such as manage/edit/add
		$mod_head = $this->load->library('module_header');
		$mod_head->setName(_('Page'));
		$mod_head->setIcon($this->icon['pages']);
		$mod_head->setLinks($links);
		$this->output = $mod_head->output();
	}

	#### CONFIG ############################################################
	function config() {

		// Prepopulate the form fields
		if (!$this->submit) $this->post = $this->common;

		// Load the form library and open a new form
		$form = $this->load->library('form');
		$form->open();

		// Descriptions
		$form->description('pages_items_per_page', _('The number of items to display per page'));

		// Form elements
		$form->text('Pages Per Page', 'pages_items_per_page', $this->post['pages_items_per_page'], NULL, 5);
		$form->button('submit', _('Update'));

		// Close Form
		$form->close();

		// Include config library, configure config and return peformed action
		$mod_conf = $this->load->library('module_config');
		$mod_conf->setName(_('Pages'));
		$mod_conf->setForm($form->output());
		$this->feedback = invoke_method($mod_conf, url(4), 'buildConfigForm');

		// Output config form or return to main function if form has been submitted
		if (!$this->submit) $this->output .= $this->feedback;
		else return $this->main();
	}

	#### MAIN ##################################################################
	function main() {

		// Get pages from database
		$page = $this->page - 1; // Change page to match array as arrays start from 0 but pages from 1
		$limit = array(
			$page * $this->common['pages_items_per_page'], // Start From
			$this->common['pages_items_per_page'] // Limit
		);
		$pages = $this->model->getPages($limit);

		// Total Pages
		$total = $this->model->getPageCount();

		// Load pagination library a build pagination links
		$pagination = $this->load->library('pagination');
		$pagination->setCount($total);
		$pagination->setUrl(url(1, 2));
		$pagination->setPerPage($this->common['pages_items_per_page']);
		$pagination->setCurrentPage($this->page);
		$pagination->buildPagination('right');

		// Load the table library and open a new table
		$table = $this->load->library('table');
		$table->open();
		$table->tr(
			$table->th(_('Name')).
			$table->th(_('Visible To'), 160, TRUE).
			$table->th(_('Actions'), 60, TRUE)
			);

		// Loop through out pages and build our table rows
		if ($pages) foreach ($pages as $page) {

			$table_name = $this->icon['pages'].' <a href="'.url(2).'/'.$page['sef_title'].'">'.$page['title'].'</a>';
			$table_visible_to = get_allowed_to($page['visible_to']);

			$table_actions = '<a href="'.url(1, 2).'/toggle-status/'.$page['id'].'" title="'.('Toggle Status').'">'.
				($page['disabled'] ? $this->icon['disabled'] : $this->icon['enabled']).'</a> ';
			$table_actions .= '<a href="'.url(1, 2).'/edit/'.$page['id'].'" title="'._('Edit').'">'.$this->icon['edit'].'</a> ';
			$table_actions .= '<a href="'.url(1, 2).'/delete/'.$page['id'].'" title="'._('Delete').'">'.$this->icon['delete'].'</a> ';

			$table->tr(
				$table->td($table_name).
				$table->td($table_visible_to, TRUE).
				$table->td($table_actions, TRUE)
				);
		}

		// Add an page link
		$table->tr($table->td($this->icon['add'].' <a href="'.url(1, 2).'/add">'._('Add a Page').'</a>', TRUE, 3), FALSE, 'footer');

		// Close table
		$table->close();

		// Set output
		$title = $this->icon['pages'].' '._('Pages');
		$body =
			$this->feedback.
			$pagination->output().
			$table->output().
			$pagination->output();

		// Send output back through box() template
		$this->output .= box($title, $body);
	}

	#### ADD ###################################################################
	function add() {

		if ($this->submit) {
			$this->model->insertPage();

			$this->feedback = notice(_('Your page has been added.'));
			return $this->main();
		}

		$title = $this->icon['add'].' '._('Add a page');
		$body = $this->buildForm('add');

		$this->output .= box($title, $body);
	}

	#### EDIT ##################################################################
	function edit() {

		if ($this->submit) {
			$this->model->updatePage($this->id);

			$this->feedback = notice(_('Your page has been edited.'));
			return $this->main();
		}

		$this->post = $this->model->getPageByID($this->id);

		$title = $this->icon['edit'].' '._('Edit page');
		$body = $this->buildForm();

		$this->output .= box($title, $body);
	}

	#### DELETE ################################################################
	function delete() {

		if ($this->confirm) {
			$this->model->deletePage($this->id);

			$this->feedback = notice(_('Your page has been deleted.'));
			return $this->main();
		}

		$page = $this->model->getPageByID($this->id);

		$links[] = array(url(1, 3).'/'.$this->id.'/confirm', $this->icon['yes'].' '._('Yes'));
		$links[] = array(url(1, 2), $this->icon['no'].' '._('No'));
		$this->feedback = notice(_('Are you sure you wish to delete').' <b>'.$page['title'].'</b>?', 'warning', $links);
		return $this->main();
	}

	#### TOGGLE STATUS #########################################################
	function toggleStatus() {

		$page = $this->model->getPageByID($this->id);

		$this->model->updatePageStatus($this->id);

		$this->feedback = notice(_('The page is now').': <b>'.($page['disabled'] ? _('Enabled') : _('Disabled')).'</b>', 'info');
		return $this->main();
	}

	#### BUILD FORM ############################################################
	function buildForm() {
		$form = $this->load->library('form');

		$form->validate('title');

		$form->open();
		$form->openFieldset();
		$form->text(_('Title').'*', 'title', $this->post['title']);
		$form->texteditor(_('Body'), 'body', $this->post['body'], 100, 100);
		$form->roles(_('Visible To'), 'visible_to', $this->post['visible_to']);
		$form->checkbox(_('Disabled'), 'disabled', $this->post['disabled']);
		$form->closeFieldset();
		$form->openSlider(_('Keywords & Description'));
		$form->textarea(_('Keywords'), 'keywords', $this->post['keywords']);
		$form->textarea(_('Description'), 'description', $this->post['description']);
		$form->closeSlider();
		$form->button('submit', (url('search', 'add') ? _('Add') : _('Edit')));
		$form->close();

		// Return the form
		return $form->output();
	}

}
?>